package edu.neusoft.yuejian_09.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import edu.neusoft.yuejian_09.R;
import edu.neusoft.yuejian_09.adapter.CommentListAdapter;
import edu.neusoft.yuejian_09.base.Constant;
import edu.neusoft.yuejian_09.base.RetrofitListener;
import edu.neusoft.yuejian_09.bean.Comment;
import edu.neusoft.yuejian_09.bean.Show;
import edu.neusoft.yuejian_09.model.CommentModel;
import edu.neusoft.yuejian_09.model.ShowModel;
import it.sephiroth.android.library.picasso.Picasso;

import static edu.neusoft.yuejian_09.base.Constant.PHOTO_URL;

/**
 * Created by 爽 on 2017/10/16.
 */

public class ShowInfoActivity extends Activity implements RetrofitListener {

    private TextView u_name,s_time,s_address,s_content;
    private ImageView img_return,u_pic,s_pic,img_remark;

    private int show_id;
    private int user_id;
    private String user_name;
    private String show_time;
    private String show_address;
    private String show_content;
    private String user_pic;
    private String show_pic;

    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        setContentView(R.layout.activity_show_info);
        initViews();
        initEvents();
        initData();
    }

    public void initViews(){
        u_name=(TextView)findViewById(R.id.user_name);
        s_address=(TextView)findViewById(R.id.address);
        s_time=(TextView)findViewById(R.id.time);
        s_content=(TextView)findViewById(R.id.content);
        img_return=(ImageView)findViewById(R.id.img_return);
        u_pic=(ImageView)findViewById(R.id.user_pic);
        s_pic=(ImageView)findViewById(R.id.show_pic);
        recyclerView= (RecyclerView)findViewById(R.id.rv_commentlist);
        img_remark=(ImageView)findViewById(R.id.remark);

    }

    public void initEvents(){
        img_return.setOnClickListener(new View.OnClickListener() {//返回
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        u_pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ShowInfoActivity.this, UserInfoActivity.class);
                Bundle bundle  = new Bundle();
                bundle.putInt("user_id",user_id);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        img_remark.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ShowInfoActivity.this, ShowRemarkActivity.class);
                Bundle bundle  = new Bundle();
                bundle.putInt("show_id",show_id);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setItemAnimator(new DefaultItemAnimator());

    }
    public void initData(){

        getShowInfo();//获取晒图详情
        getCommentList();//获取评论列表
    }

    //获取晒图信息数据
    public void getShowInfo(){

        Bundle bundle = getIntent().getExtras();
        show_id = bundle.getInt("s_id");
        user_id = bundle.getInt("user_id");
        ShowModel showModel = new ShowModel();
        showModel.getShowById(show_id,this);

    }
    //获取评论列表数据
    public void getCommentList(){
        CommentModel commentModel=new CommentModel();
        commentModel.getCommentsById(show_id,this);
    }

    @Override
    public void onSuccess(Object o, int flag) {
        switch (flag){
            case Constant.SHOWBYID:
                Show show=(Show)o;
                user_name = show.getUser_name();
                user_pic =PHOTO_URL+show.getUser_pic();
                show_address = show.getS_address();
                show_pic =PHOTO_URL+show.getS_pic();
                show_content=show.getS_content();
                show_time=show.getS_date();
                u_name.setText(user_name);
                s_content.setText(show_content);
                s_address.setText(show_address);
                s_time.setText(show_time+"");
                //t_ask.setText(train_phone+"");
                if (show_pic != "") {
                    Picasso.with(ShowInfoActivity.this)
                            .load(show_pic)
                            .into(s_pic);
                    s_pic.setScaleType(ImageView.ScaleType.FIT_XY);
                } else {
                    s_pic.setImageResource(R.drawable.no_pic);
                }
                if (user_pic != "") {
                    Picasso.with(ShowInfoActivity.this)
                            .load(user_pic)
                            .into(u_pic);
                    u_pic.setScaleType(ImageView.ScaleType.FIT_XY);
                } else {
                    u_pic.setImageResource(R.drawable.no_pic);
                }

                break;
            case Constant.COMMENT:
                List<Comment> data=(List<Comment>) o;
                recyclerView.setAdapter(new CommentListAdapter(ShowInfoActivity.this,data));
                break;
        }
    }

    @Override
    public void onFail(){ Toast.makeText(ShowInfoActivity.this, "网络发生错误", Toast.LENGTH_SHORT).show();}


}
